#!/bin/bash

#Submit this script with: sbatch thefilename

#SBATCH --time=720:00:00   # walltime
#SBATCH --ntasks-per-node=8
#SBATCH --nodes=1   # number of nodes
#SBATCH --mem=50G   # memory per node
#SBATCH -J "orca"   # job name
#SBATCH --partition=vasp # do not change
#SBATCH --qos=vasp # do not change


# set input file name here
export FullNameInputFile=benzene-Br-TD-PBE0-SP.inp

jobname=`echo ${FullNameInputFile} | cut -f 1 -d. `
export RSH_COMMAND="/usr/bin/ssh -x"

# LOAD MODULES, INSERT CODE, AND RUN YOUR PROGRAMS HERE
ulimit -d unlimited
ulimit -s unlimited
ulimit -t unlimited
ulimit -v unlimited

export SLURM_EXPORT_ENV=ALL


cd $SLURM_SUBMIT_DIR
username=`whoami`

### here, suppose /tmp is the I/O storage. 

ORCA_RUNDIR=/tmp/${username}.${SLURM_JOB_ID}

if [ ! -a $ORCA_RUNDIR ]; then
   echo "Scratch directory $ORCA_RUNDIR created."
   mkdir -p $ORCA_RUNDIR
fi

cp -r $SLURM_SUBMIT_DIR/* $ORCA_RUNDIR

cd $ORCA_RUNDIR




export EXE="/opt/hpc4you/apps/orca/orca_v5.0.4.EXE"

echo "Starting ORCA run at `hostname` on:" `date` >> ${SLURM_JOB_ID}.log

${EXE} ${jobname}.inp > ${jobname}.out &
PID=$!
while kill -0 $PID 2> /dev/null; do
rsync -av --exclude=*.inp --exclude=*.tmp --exclude=oldorb.gbw  ${ORCA_RUNDIR}/*  $SLURM_SUBMIT_DIR &> /dev/null
sleep 5
done


echo "Finished ORCA run at `hostname `on:" `date` >> ${SLURM_JOB_ID}.log
cat $SLURM_JOB_NODELIST >> ${SLURM_JOB_ID}.log

mv $ORCA_RUNDIR/* $SLURM_SUBMIT_DIR
rm -fr $ORCA_RUNDIR

